package com.etime.shycourse.controller.wx.applets.controller;


import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.pojo.Coursescore;
import com.etime.shycourse.services.interfaces.CoursescoreServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 备注：一个用户只能给课程打一次分。分数可以修改，但是只有一条记录 前端控制器
 * </p>
 *
 * @author wb
 * @since 2022-03-04
 */
@Api(tags = "微信课程评分管理数据接口")
@RestController
@RequestMapping("/wx/coursescore")
public class WxCoursescoreController {
    @Autowired
    private CoursescoreServices services;

    @ApiOperation("课程评分信息接口")
    @ApiImplicitParam("携带token,和课程id 进行修改或添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ccourseid", value = "必传值 课程的id"),
    })
    @PostMapping("/insetOrUpdate")
    public BaseMessage insertOrUpdateCourseScore(HttpServletRequest request, @RequestBody Coursescore coursescore) {
        String token = request.getHeader("token");
        int rtn = services.insertOrUpdateCourseScore(token, coursescore);
        BaseMessage msg = new BaseMessage();
        msg.setCode(rtn);
        if (rtn > 0) {
            msg.setMsg("修改或插入课程评分成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改或插入课程评分失败");
        }
        return msg;
    }

    @ApiOperation("携带token,和课程id,查询用户对课程的评分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ccourseid", value = "必传值 课程的id"),
    })
    @GetMapping("/get")
    public PojoMessage<Coursescore> selectCoreByUserIdCourseId(HttpServletRequest request, Coursescore coursescore) {
        String token = request.getHeader ("token");
        PojoMessage<Coursescore> msg = new PojoMessage<> ();
        Coursescore data = services.selectCoreByUserIdCourseId (token, coursescore);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("查询一条评论、提问失败");
        }
        return msg;
    }
}

